//
//  MonthDetailController.m
//  OnePiece
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 wsy. All rights reserved.
//
#define FIRST @"http://api.breadtrip.com/destination/place/5/"


#import "SceneryDetailController.h"

#import "SceneryDetailCell.h"
#import "SceneryDetailModel.h"

#import "OPRequesterManager.h"
#import <UIImageView+WebCache.h>


#import "UMSocial.h"
@interface SceneryDetailController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,UMSocialUIDelegate>
@property (nonatomic, strong)UITableView *mainTableView;
@property (nonatomic, strong)SceneryDetailModel *model;

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIPageControl *pageControl;

@property (nonatomic, strong)UIView *headView;
@property (nonatomic, strong)UIImageView *scrollImageView;
@property (nonatomic, strong)NSMutableArray *imageArray;

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *adviseLabel;

@end

@implementation SceneryDetailController

-(NSMutableArray *)imageArray
{
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _imageArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.leftButton setImage:[UIImage imageNamed:@"meishi-back"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shareButton.frame = CGRectMake(SCREEN_WIDTH - 45, 15, 20, 20);
    [shareButton setImage:[UIImage imageNamed:@"meishi-share"] forState:(UIControlStateNormal)];
    [shareButton addTarget:self action:@selector(shareInfomation) forControlEvents:(UIControlEventTouchUpInside)];
    [self.topView addSubview:shareButton];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-114) style:UITableViewStylePlain];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    
    [self.view addSubview:_mainTableView];
    
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 290)];
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 190, 200, 30)];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.textColor = [UIColor grayColor];
    [_headView addSubview:_nameLabel];
    
    _adviseLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-150, 220, 300, 60)];
    _adviseLabel.numberOfLines = 3;
    _adviseLabel.font = [UIFont systemFontOfSize:14];
    _adviseLabel.textAlignment = NSTextAlignmentCenter;
    _adviseLabel.textColor = [UIColor grayColor];

    [_headView addSubview:_adviseLabel];
    
    _mainTableView.tableHeaderView = _headView;
    _mainTableView.bounces = NO;
    _mainTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
       
    [_mainTableView registerClass:[SceneryDetailCell class] forCellReuseIdentifier:@"detail"];

    [self requestData];
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)shareInfomation{
    
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"562dcfa4e0f55a2a14004075"
                                      shareText:[NSString stringWithFormat:@"#旅游景点#%@", self.model.name]
                                     shareImage: [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.model.share_args[@"default"][@"shr_image"]]]]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];
    
}


- (void)requestData
{
    __weak SceneryDetailController *myself = self;
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    [OPRequesterManager requestWithType:GET urlString:[NSString stringWithFormat:@"%@%ld/",FIRST,self.detailId] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
       
    
        
        
            _model = [[SceneryDetailModel alloc]init];
            [_model setValuesForKeysWithDictionary:dic];
        _nameLabel.text = myself.model.name;
        _adviseLabel.text = myself.model.recommended_reason;
        self.titleLable.text = myself.model.name;
        
        NSArray *arr = dic[@"hottest_places"];
       
        for (NSDictionary *imageDic in arr) {
            [myself.imageArray addObject:imageDic[@"photo"]];
        }
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180)];
        _scrollView.delegate = myself;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * _imageArray.count, 180);
        _scrollView.pagingEnabled = YES;
        
        for (int i = 0; i <_imageArray.count; i++) {
            _scrollImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, 180)];
            
            [_scrollImageView sd_setImageWithURL:[NSURL URLWithString:_imageArray[i]]];

            [_scrollView addSubview:_scrollImageView];
        }
        
        [_headView addSubview:_scrollView];
        
        
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-120, 150, 100, 30)];
        _pageControl.numberOfPages = _imageArray.count;
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
        [_headView addSubview:_pageControl];
        
        
        [myself.mainTableView reloadData];
        
        
    } error:^(NSError *error) {
        NSLog(@"--------------景点详情界面数据请求失败---------------");
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage = _scrollView.contentOffset.x/SCREEN_WIDTH;
    
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SceneryDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
    
    switch (indexPath.row) {
        case 0:{
            cell.lineNameLabel.text = @"概况";
            cell.contentLabel.text = self.model.describe;
            cell.contentLabel.frame = CGRectMake(10, 30, SCREEN_WIDTH-20, [SceneryDetailCell heightOfString:self.model.describe]);

        }
            break;
        case 1:{
            cell.lineNameLabel.text = @"地址";
            cell.contentLabel.text = self.model.address;
            cell.contentLabel.frame = CGRectMake(10, 30, SCREEN_WIDTH-20, [SceneryDetailCell heightOfString:self.model.address]);
        }
            break;
        case 2:{
            cell.lineNameLabel.text = @"行动路线";
            cell.contentLabel.text = self.model.arrival_type;
            cell.contentLabel.frame = CGRectMake(10, 30, SCREEN_WIDTH-20, [SceneryDetailCell heightOfString:self.model.arrival_type]);
        }
            break;
        case 3:{
            cell.lineNameLabel.text = @"开放时间";
            cell.contentLabel.text = self.model.opening_time;
            cell.contentLabel.frame = CGRectMake(10, 30, SCREEN_WIDTH-20, [SceneryDetailCell heightOfString:self.model.opening_time]);
        }
            break;
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row)
    {
        case 0:{
    
            return 40 + [SceneryDetailCell heightOfString:self.model.describe];
        }
            break;
        case 1:{
            return 40 + [SceneryDetailCell heightOfString:self.model.address];
        }
            break;
        case 2:{
            return 40 + [SceneryDetailCell heightOfString:self.model.arrival_type];
        }
            break;
        case 3:{
            return 40 + [SceneryDetailCell heightOfString:self.model.opening_time];
        }
            break;
            
        default:{
            return 0;
        }
            break;
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
